#include<stdio.h>
#include<conio.h>
#define MAX 11000
int a[MAX+1][MAX+1];
int queuex[1000009],queuey[1000001];
int stop[MAX][MAX];
int m,n,top;
int dirx[]={0,-1,0,1,0};
int diry[]={0,0,1,0,-1};
int STOP[]={0,3,4,1,2};
void input(){
    int i,j;
    FILE *fp=fopen("MAZE.INP","r");
    fscanf(fp,"%d%d",&m,&n);
    fscanf(fp,"%c",&a[0][0]);
    for(i=1;i<=m;i++){
        for(j=1;j<=n+1;j++){
            fscanf(fp,"%c",&a[i][j]);   
            a[i][j]=(a[i][j]!='.');
        }
    }
    for(i=0;i<=m+1;i++)a[i][0]=a[i][n+1]=1;
    for(i=0;i<=n+1;i++)a[0][i]=a[m+1][i]=1;
    fclose(fp);
}
int dfs(int index,int x,int y){
    int u,v,i;
    top=1;
    queuey[top]=y;
    queuex[top]=x;
    do{
        u=queuey[top];
        v=queuex[top];
        top--;
        a[u][v]=index;
        for(i=1;i<=4;i++){
            if((stop[u][v])&(1<<i))continue;
            if(a[u+diry[i]][v+dirx[i]]==0){
                    top++;
                    queuex[top]=v;
                    queuey[top]=u;
                    top++;
                    queuex[top]=v+dirx[i];
                    queuey[top]=u+diry[i];
                    a[u+diry[i]][v+dirx[i]]=index;
                    stop[u][v]|=(1<<i);
                    stop[u+diry[i]][v+dirx[i]]|=(1<<STOP[i]);
            }else if(a[u+diry[i]][v+dirx[i]]==index){
                return 0;
            }
        }
    }while(top>0);
    return 1;
}
void output(int result){
    FILE *fp=fopen("MAZE.OUT","w");
    fprintf(fp,"%d",result);
    fclose(fp);
}
int process(){
    int k=2;
    for(int i=1;i<=m;i++)
        for(int j=1;j<=n;j++)
            if(a[i][j]==0)if(dfs(++k,i,j)==0)return 0;
    return 1;
}
int main(){
    input();
    output(process());
    return 0;
}
    
    
